package com.server.llh.llhserverwork.activity.my;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.family.llh.lelaohuitoastview.dialog.toast.T;
import com.lelaohui.net.listener.NetDataListener;
import com.lelaohui.net.netmanager.INetRequestManager;
import com.lelaohui.net.netmanager.NetRequestManager;
import com.lelaohui.net.request.LRequest;
import com.lelaohui.net.utils.UrlsTools;
import com.llh.llhsuppliers.R;
import com.server.llh.llhserverwork.activity.base.LlhBaseActivity;
import com.server.llh.llhserverwork.bean.Constants;
import com.server.llh.llhserverwork.utils.Md5;
import com.server.llh.llhserverwork.utils.ServerPort;
import com.server.llh.llhserverwork.utils.SharedPreferenceUtil;
import com.server.llh.llhserverwork.view.CleanableEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ZTF on 2017-06-06.
 * 修改密码
 */

public class UpdatePwdActivity extends LlhBaseActivity {
    @BindView(R.id.titleName)
    TextView titleName;
    @BindView(R.id.main_back)
    ImageButton mainBack;
    @BindView(R.id.leftBtn_RL_title_base)
    RelativeLayout leftBtnRLTitleBase;
    @BindView(R.id.serverName)
    TextView serverName;
    @BindView(R.id.linearLayout1)
    LinearLayout linearLayout1;
    @BindView(R.id.oldPwd)
    CleanableEditText oldPwd;
    @BindView(R.id.newPwd)
    CleanableEditText newPwd;
    @BindView(R.id.btnOk)

    Button btnOk;
    private String userId,token;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        titleName.setText("修改密码");
        userId= SharedPreferenceUtil.getUserInfo(Constants.UserInfo.USER_ID,getApplicationContext());
        token= SharedPreferenceUtil.getUserInfo(Constants.UserInfo.TOKEN_STR,getApplicationContext());
    }

    @OnClick({R.id.main_back, R.id.btnOk})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.main_back:
                onBackPressed();
                break;
            case R.id.btnOk:
                String oldPwdStr=oldPwd.getText().toString();
                String newPwdStr=newPwd.getText().toString();
                if (TextUtils.isEmpty(newPwdStr)||TextUtils.isEmpty(oldPwdStr)){
                    T.showAnimToast(getApplicationContext(),"新密码或老密码不能为空！");
                    return;
                }else if (newPwdStr.equals(oldPwdStr)){
                    T.showAnimToast(getApplicationContext(),"新密码与老密码不能相同！");
                    return;
                }
                reqUpdate(userId,token,oldPwdStr,newPwdStr);
                break;
        }
    }

    /**
     * 发送请求
     * @param userId
     * @param oldPwd
     * @param newPwd
     */
   public void reqUpdate(String userId,String token,String oldPwd,String newPwd){
       LRequest lRequest = new LRequest();
       lRequest.addParam("userId", userId);
       lRequest.addParam("token", token);
       lRequest.addParam("loginPassword", Md5.md5s(newPwd));
       lRequest.addParam("oldPassWord",Md5.md5s(oldPwd));
       lRequest.setUri(ServerPort.UPDATE_PWD_PRO);
       lRequest.setUriType("");
       lRequest.setIp(UrlsTools.REQ_URL);
       lRequest.setType(String.class);
       requestData(lRequest, new NetDataListener<String>() {
           @Override
           public void onError(Throwable t) {

               T.showAnimErrorToast(getApplicationContext(),"服务器繁忙!");
           }
           @Override
           public void onNext(String responseBean,String code) {
                if ("0".equals(code)){
                    T.showAnimToast(getApplication().getApplicationContext(),"修改密码成功！");
                    onBackPressed();
                }else{
                    T.showAnimToast(getApplication().getApplicationContext(),"修改密码失败！");
                }
           }
       });

   }
    @Override
    public void showProgress() {
        try {
            showProgress(UpdatePwdActivity.this);
        } catch (Exception e) {

        }

    }

    @Override
    protected INetRequestManager getRequestManager() {
        return NetRequestManager.getInstance();
    }

    @Override
    public void hideProgress() {
        closeProgress();
    }

    @Override
    protected int getlayout() {
        return R.layout.activity_update_pwd;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void doBasess() {

    }


}
